﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Data;
using Nvigorate.Data.Query;
using Nvigorate.Relational.Mapping.Index;
using Nvigorate.Relational.Navigation;
using Nvigorate.Relational.Repository;
using Nvigorate.Relational.Visitors;
using Nvigorate.TestObjects;

namespace Nvigorate.Test.Relational.Repository
{
    /// <summary>
    /// Summary description for SelectVisitorTests
    /// </summary>
    [TestClass]
    public class SelectVisitorTests
    {
        #region Hide

        public SelectVisitorTests()
        {
            //
            // TODO: Add constructor logic here
            //
            MapFake.PopulateMaps();
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #endregion
        
        [TestMethod]
        public void SelectPerson1()
        {
            var maps = new RepositoryMapIndex(new MapIndex(), "simple");
            var instruction = new LoadInstruction<Person>()
                .Where(Criterion.Property("FirstName").Like("A%") & Criterion.Property("LastName").Like("R%"));
            var visitor = new SelectVisitor(
                instruction,
                new MSSQL9Provider(),
                maps
                );

            var navigator = new PersistableTypeNavigator(typeof (Person), instruction, maps);
            navigator.Accept(visitor);
            var result = visitor.GetQuery();
            new Parameterizer().Parameterize(result, new MSSQL9Provider(), new MapIndex().GetRepositoryMapIndex("simple"));
            var query = new MSSQL9Visitor().Translate(result);

            Assert.IsNotNull(query);
        }
    }
}
